package com.accounting.client.frontend.product.updates;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.PurchaseRecord;
import com.accounting.client.remote.transfer.UpdatePurchaseRecordRequest;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.resource.CommonResources;
import corelib.client.ui.SimpleModal;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.DateBox;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.layout.LabelAlignment;
import corelib.client.ui.widget.layout.RelativeLayout;
import corelib.client.ui.widget.layout.RelativeRow;

public class UpdateRecordDateHandler extends SimpleDialogHandler {

	private PurchaseRecord record;
	private DateBox dateField = new DateBox();
	
	public UpdateRecordDateHandler(PurchaseRecord record){
		this.record = record;
	}
	
	@Override
	public String getHeading() {
		return record.getProductName() + " " + "更新购买日期";
	}
	
	public PurchaseRecord getRecord() {
		return this.record;
	}

	@Override
	public Size getSize() {
		return new Size(400, 150);
	}

	@Override
	public Widget getContentPanel() {
		int fw = 150;
		int lw = 100;
		RelativeLayout layout = RelativeLayout.create(fw, LabelAlignment.Left, lw);
		
		RelativeRow row = new RelativeRow();
		row.add(new SimpleLabel("购买日期").setBold(true), dateField, layout);
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		panel.add(row.getPanel());
		return panel;
	}
	
	
	@Override
	public void onShowing() {
		super.onShowing();
		dateField.setDate(this.record.getDateCreated());
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[]{
			 new IsButton("确认",CommonResources.INSTANCE.accept16(),100, new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					 update();
				}
			 }),
			 new IsButton("关闭",BoxResources.INSTANCE.cancel16(), 100, new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						closeDialog();
					}
			 })
		};
	}

	@Override
	public boolean isCloseable() {
		return false;
	}
	
	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	private void update() {
		closeDialog();
		
		SimpleModal.show("更新。。。");;
		UpdatePurchaseRecordRequest req = new UpdatePurchaseRecordRequest();
		req.setId(this.record.getId());
		req.setDatePurchased(dateField.getDate());
		
		Services.ProductService.UpdatePurchaseRecord(SessionManager.getPassport(), req,
				new AsyncCallback<Boolean>(){
					@Override
					public void onFailure(Throwable caught) {
						SimpleModal.hide();
						ErrorHandler.handleServiceError(caught);
					}

					@Override
					public void onSuccess(Boolean result) {
						SimpleModal.hide();
					}
			
		});
	}

}
